<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="登录账号" prop="accountId">
				{{form.accountId}}
			</el-form-item>
			<el-form-item label="姓名" prop="userName">
				{{form1.userName}}
			</el-form-item>
				<el-form-item label="新密码" prop="password">
					<el-input type="password" v-model="form.password" clearable show-password></el-input>
				</el-form-item>
				<el-form-item label="确认密码" prop="password2">
					<el-input type="password" v-model="form.password2" clearable show-password></el-input>
				</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "resetPwd",
			titleMap: {
				resetPwd: '密码重置'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form1: {
				userName: "",
			},
			form:{
				accountId:"",
				password:"",
				password2:""
			},
			rules: {
				password: [
					{required: true, message: '请输入登录密码'},
					{validator: (rule, value, callback) => {
							if (this.form.password2 !== '') {
								this.$refs.dialogForm.validateField('password2');
							}
							callback();
						}}
				],
				password2: [
					{required: true, message: '请再次输入密码'},
					{validator: (rule, value, callback) => {
							if (value !== this.form.password) {
								callback(new Error('两次输入密码不一致!'));
							}else{
								callback();
							}
						}}
				]
			},
		}
	},
	mounted() {
	},
	methods: {
		//表单提交方法
		open(){
			this.mode = "resetPwd";
			this.visible = true;
			return this
		},
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.system.user.resetNewPassWord.post(this.form);
					this.isSaveing = false;
					if(res.code == 200){
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}else{
					return false;
				}
			})
		},
		//表单注入数据
		setData(data){
			this.form.accountId = data.accountId;
			this.form1.userName = data.userName;
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
